package main

import (
	"context"
	"flag"
	"fmt"
	"log"
	"net"

	"grpcLoadBalancer/pb"

	"google.golang.org/grpc"
	"google.golang.org/protobuf/types/known/wrapperspb"
)

type echoServer struct {
	addr string
}

func (s *echoServer) EchoRequest(ctx context.Context, value *wrapperspb.StringValue) (*wrapperspb.StringValue, error) {
	return &wrapperspb.StringValue{Value: s.addr + ":" + value.Value}, nil
}

func main() {
	var port int
	flag.IntVar(&port, "port", 80, "port")
	flag.Parse()
	addr := fmt.Sprintf(":%d", port)
	conn, err := net.Listen("tcp", addr)
	if err != nil {
		log.Fatalf("net.Listen err: %v", err)
	}
	defer conn.Close()
	server := grpc.NewServer()
	pb.RegisterEchoManagementServer(server, &echoServer{addr: addr})
	if err := server.Serve(conn); err != nil {
		log.Fatalf("server.Serve err: %v", err)
	}
}
